package com.example.avmanagerweb.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.avmanagerweb.entity.Audiogroup;
import com.example.avmanagerweb.entity.Audiogroupinpt;
import com.example.avmanagerweb.entity.Audiogroupstaff;
import com.example.avmanagerweb.entity.SysUserinfo;
import com.example.avmanagerweb.entity.vo.AudiogroupVo;
import com.example.avmanagerweb.entity.vo.AudiogroupsetVo;
import com.example.avmanagerweb.redisService.RedisService;
import com.example.avmanagerweb.result.RLIST;
import com.example.avmanagerweb.service.AudiogroupService;
import com.example.avmanagerweb.service.AudiogroupinptService;
import com.example.avmanagerweb.service.AudiogroupstaffService;
import com.example.avmanagerweb.service.SysUserinfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2023-08-16
 */
@RestController
@RequestMapping("/audiogroupstaff")
public class AudiogroupstaffController {

    @Autowired
    private AudiogroupstaffService audiogroupstaffService;
    @Autowired




    private AudiogroupService audiogroupService;

    @Autowired
    private SysUserinfoService sysUserinfoService;

    @Autowired
    private AudiogroupinptService audiogroupinptService;

    @Autowired
    private RedisService redisService;


    @PostMapping(value = "/alterMember")
    public RLIST addGroup(@RequestBody AudiogroupsetVo audiogroupsetVo, HttpServletRequest request){
        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        System.out.println(value);
        if( value == null || value == 0 ) {
            return RLIST.error().message("token异常 出现bug");
        }
        String  json = (String) redisService.getValue(value+"");
        JSONObject jsonObject = JSONObject.parseObject(json);
        SysUserinfo sysUserinfo = JSON.toJavaObject(jsonObject, SysUserinfo.class);

        if(audiogroupsetVo.getName() != null && (!audiogroupsetVo.getName().equals("")) ){
            Audiogroup audiogroup = new Audiogroup();
            audiogroup.setAudiogroupid(Long.valueOf(audiogroupsetVo.getGroupId()));
            audiogroup.setAudiogroupname(audiogroupsetVo.getName());

            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("AudioGroupID",audiogroup.getAudiogroupid());
            wrapper.eq("AdminID",sysUserinfo.getUid());
            boolean b = audiogroupService.update(audiogroup,wrapper);
            if(!b){
                return RLIST.error().message("组名修改失败");
            }
        }


        QueryWrapper wrapper2 = new QueryWrapper();
        wrapper2.eq("ID",audiogroupsetVo.getGroupId());
        wrapper2.eq("AdminID",sysUserinfo.getUid());

        int count1 = audiogroupinptService.count(wrapper2);
        System.out.println(count1);
        if (count1!=1) {
            return RLIST.error().message("组不存在");
        }


        if (sysUserinfo == null) {
            return RLIST.error().message("无法匹配管理员ID异常");
        }

        if ( (sysUserinfo.getUissysmana() == null || !sysUserinfo.getUissysmana()) && (sysUserinfo.getUisdepartmana() == null || !sysUserinfo.getUisdepartmana())) {
            return RLIST.error().message("账号权限不够");
        }



        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("AudioGroupID", audiogroupsetVo.getGroupId());
        int count = audiogroupstaffService.count(wrapper);
        boolean b1 = false;
        if (count > 0) {
            b1 = audiogroupstaffService.remove(wrapper);
        }

        //删除成功就可以修改
        if (b1 || count >= 0) {
            if (audiogroupsetVo.getUserArr() != null && audiogroupsetVo.getUserArr().length != 0) {
                List<Audiogroupstaff> audiogroupstaffs = new ArrayList<>();
                Integer[] userArr = audiogroupsetVo.getUserArr();
                for (Integer integer : userArr) {
                    Audiogroupstaff audiogroupstaff = new Audiogroupstaff();
                    audiogroupstaff.setAudiogroupid((long) audiogroupsetVo.getGroupId());
                    audiogroupstaff.setUserid(integer);

                    audiogroupstaffs.add(audiogroupstaff);
                }
                boolean b = audiogroupstaffService.saveBatch2(audiogroupstaffs);
                if (!b) {
                    return RLIST.error().message("成员添加失败");
                } else {
                    return RLIST.ok();
                }
            } else {
                return RLIST.ok();
            }
        }


        return RLIST.error();
    }

}

